/**============================================================================
 Confidential Information - Limited distribution to authorized persons only.
 This software is protected as an unpublished work under the U.S. Copyright
 Act of 1976.

 Copyright (c) 2011, BluePhoenix Solutions, Inc. All rights reserved.
 ----------------------------------------------------------------------------
 file......: NotepadEditorWindow.java
 Author....: Vyacheslav Kovalyov
 Created...: 23.10.11

 Description:

 ============================================================================*/
package ru.monyag.pred.swing.ui.notepad;

import ru.monyag.pred.core.notepad.Note;
import ru.monyag.pred.core.tag.Tag;
import ru.monyag.pred.swing.ui.BasePanel;
import ru.monyag.pred.swing.ui.buttons.BaseSwingButton;
import ru.monyag.pred.swing.ui.listeners.IUpdateListener;
import ru.monyag.pred.swing.ui.windows.Base2ButtonsDialog;

import javax.swing.*;
import javax.swing.text.JTextComponent;

import static java.awt.BorderLayout.*;
import static ru.monyag.pred.core.utils.PredCoreUtils.getString;
import static ru.monyag.pred.swing.util.PredSwingConstants.*;

/**
 * @author Vyacheslav Kovalyov
 * @version 1.0
 */
public class NotepadEditorWindow extends Base2ButtonsDialog implements IUpdateListener<Tag> {

    public NotepadEditorWindow(Note note, IUpdateListener<Note> listener) {
        super(getString("notepad.editor.title"), DESKTOP_WIDTH, DESKTOP_HEIGHT);

        this.note = note;
        this.listener = listener;

        initTitlePanel();
        initContentPanel();
        initTagsPanel();

        addButton(new BaseSwingButton(CLOUD_ICON) {
            @Override
            public void onClick() {
                new NotepadCloudDialog(NotepadEditorWindow.this).setVisible(true);
            }
        });
    }

    @Override
    public void dispatchUpdate(Tag object) {
        note.addTag(object.getText());
        tags.setText(note.getTagsString());
    }

    @Override
    protected void onCancel() {
        dispose();
    }

    @Override
    protected void onApply() {

        note.setTitle(header.getText());
        note.setMessage(body.getText());
        note.setTags(tags.getText());
        listener.dispatchUpdate(note);

        dispose();
    }

    private void initTitlePanel() {
        header = new JTextField();
        header.setText(note.getTitle());
        addComponent(decorate(header, getString("notepad.title.label")), NORTH);
    }

    private void initContentPanel() {
        body = new JTextPane();
        body.setText(note.getMessage());
        addComponent(decorate(body, getString("notepad.body.label"), true), CENTER);
    }

    private void initTagsPanel() {
        tags = new JTextField();
        tags.setText(note.getTagsString());
        addComponent(decorate(tags, getString("notepad.tags.label")), SOUTH);
    }

    private JPanel decorate(JTextComponent area, String text) {
        return decorate(area, text, false);
    }

    private JPanel decorate(JTextComponent area, String text, boolean scrollable) {
        BasePanel panel = new BasePanel();
        panel.add(new JLabel(text), NORTH);

        if (scrollable) {
            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setLayout(new ScrollPaneLayout());
            scrollPane.setViewportView(area);

            panel.add(scrollPane, CENTER);
        } else {

            panel.add(area, CENTER);
        }

        return panel;
    }

    private JTextField header;
    private JTextPane body;
    private JTextField tags;
    private Note note;
    private IUpdateListener<Note> listener;
}
